﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace CarolLib.PostCrawler.Model
{
    /// <summary>
    /// 类t_Thread。
    /// </summary>
    [Serializable]
    public partial class TThread
    {
        public TThread()
        { }

        
		/// <summary>
		/// Thread OR映射构造函数
		/// </summary>
        public TThread(System.Data.IDataReader dr)
		{
			this.ThreadID = (Guid)dr["ThreadID"];
			this.ThreadNo = Convert.ToInt32(dr["ThreadNo"]);
			if(!Convert.IsDBNull(dr["ThreadTitle"]))
				this.ThreadTitle = Convert.ToString(dr["ThreadTitle"]);
		}

        #region Model
        private Guid _threadid;
        private int _threadno;
        private string _threadtitle;
        /// <summary>
        /// 
        /// </summary>
        public Guid ThreadID
        {
            set { _threadid = value; }
            get { return _threadid; }
        }
        /// <summary>
        /// 
        /// </summary>
        public int ThreadNo
        {
            set { _threadno = value; }
            get { return _threadno; }
        }
        /// <summary>
        /// 
        /// </summary>
        public string ThreadTitle
        {
            set { _threadtitle = value; }
            get { return _threadtitle; }
        }
        #endregion Model

        #region  Method

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public TThread(Guid ThreadID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ThreadID,ThreadNo,ThreadTitle ");
            strSql.Append(" FROM [t_Thread] ");
            strSql.Append(" where ThreadID=@ThreadID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ThreadID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ThreadID"] != null && ds.Tables[0].Rows[0]["ThreadID"].ToString() != "")
                {
                    this.ThreadID = Guid.Parse(ds.Tables[0].Rows[0]["ThreadID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadNo"] != null && ds.Tables[0].Rows[0]["ThreadNo"].ToString() != "")
                {
                    this.ThreadNo = int.Parse(ds.Tables[0].Rows[0]["ThreadNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadTitle"] != null && ds.Tables[0].Rows[0]["ThreadTitle"].ToString() != "")
                {
                    this.ThreadTitle = ds.Tables[0].Rows[0]["ThreadTitle"].ToString();
                }
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(Guid ThreadID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from [t_Thread]");
            strSql.Append(" where ThreadID=@ThreadID ");

            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ThreadID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }


        /// <summary>
        /// 增加一条数据
        /// </summary>
        public void Add()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into [t_Thread] (");
            strSql.Append("ThreadID,ThreadNo,ThreadTitle)");
            strSql.Append(" values (");
            strSql.Append("@ThreadID,@ThreadNo,@ThreadTitle)");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ThreadNo", SqlDbType.Int,4),
					new SqlParameter("@ThreadTitle", SqlDbType.NVarChar,50)};
            parameters[0].Value = ThreadID;
            parameters[1].Value = ThreadNo;
            parameters[2].Value = ThreadTitle;

            DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
        }
        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update()
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update [t_Thread] set ");
            strSql.Append("ThreadID=@ThreadID,");
            strSql.Append("ThreadNo=@ThreadNo,");
            strSql.Append("ThreadTitle=@ThreadTitle");
            strSql.Append(" where ThreadID=@ThreadID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier,16),
					new SqlParameter("@ThreadNo", SqlDbType.Int,4),
					new SqlParameter("@ThreadTitle", SqlDbType.NVarChar,50)};
            parameters[0].Value = ThreadID;
            parameters[1].Value = ThreadNo;
            parameters[2].Value = ThreadTitle;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(Guid ThreadID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from [t_Thread] ");
            strSql.Append(" where ThreadID=@ThreadID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ThreadID;

            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public void GetModel(Guid ThreadID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select ThreadID,ThreadNo,ThreadTitle ");
            strSql.Append(" FROM [t_Thread] ");
            strSql.Append(" where ThreadID=@ThreadID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ThreadID", SqlDbType.UniqueIdentifier)};
            parameters[0].Value = ThreadID;

            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["ThreadID"] != null && ds.Tables[0].Rows[0]["ThreadID"].ToString() != "")
                {
                    this.ThreadID = new Guid(ds.Tables[0].Rows[0]["ThreadID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadNo"] != null && ds.Tables[0].Rows[0]["ThreadNo"].ToString() != "")
                {
                    this.ThreadNo = int.Parse(ds.Tables[0].Rows[0]["ThreadNo"].ToString());
                }
                if (ds.Tables[0].Rows[0]["ThreadTitle"] != null && ds.Tables[0].Rows[0]["ThreadTitle"].ToString() != "")
                {
                    this.ThreadTitle = ds.Tables[0].Rows[0]["ThreadTitle"].ToString();
                }
            }
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public List<TThread> GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM [t_Thread] ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            using (var dr = DbHelperSQL.ExecuteReader(strSql.ToString()))
            {
                List<TThread> result = new List<TThread>();

                while (dr.Read())
                {
                    result.Add(new TThread(dr));
                }

                dr.Close();
                return result;
            }
        }

        #endregion  Method
    }
}
